package com.crrc.sandameter.view

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import com.cj.mvvmlib.base.activity.BaseVmActivity
import com.cj.mvvmlib.base.viewmodel.BaseViewModel
import com.cj.mvvmlib.util.ToastUtil
import com.crrc.sandameter.Constant
import com.crrc.sandameter.DialogUtils
import com.crrc.sandameter.R
import com.crrc.sandameter.common.LogFileUtils
import com.crrc.sandameter.common.SPUtil
import com.crrc.sandameter.databinding.ActivityRegisterDeviceBinding
import com.crrc.sandameter.http.API
import com.crrc.sandameter.http.GET
import com.crrc.sandameter.http.http
import com.crrc.sandameter.http.parseObject

/**
 * 设备认证页面
 */
class RegisterDeviceActivity : BaseVmActivity<BaseViewModel>() {

    private lateinit var registerDeviceBinding: ActivityRegisterDeviceBinding

    override fun useTitleBar(): Boolean {
        return false
    }

    override fun layoutId(): View {
        registerDeviceBinding = ActivityRegisterDeviceBinding.inflate(layoutInflater)
        return registerDeviceBinding.root
    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {

    }

    override fun setListener() {
        registerDeviceBinding.registerBt.setOnClickListener {
            val mac = registerDeviceBinding.macAddressEt.text.toString()
            if (!isValidMacAddress(mac)) {
                ToastUtil.showShortToast("请输入正确的 MAC 地址")
                return@setOnClickListener
            }
            http {
                url = API.getBaseUrl() + API.DEVICE_AUTH
                requestType = GET
                paramters["equipType"] = "AS33"
                paramters["equipMac"] = mac
                success = {
                    LogFileUtils.writeTxtToFile("设备认证返回数据：$it")
                    val authNo = parseObject<LoginActivity.AuthNo>(it).authNo
                    SPUtil.put(Constant.isFirstInstall, false)
                    SPUtil.put(Constant.authNo, authNo) //保存认证编码
                    finish()
                }
                fail = { code, message ->
                    Log.d("TAG", "code: $code, message: $message")
                    ToastUtil.showShortToast("设备认证失败返回数据：$message")
                }
            }
        }
        registerDeviceBinding.calibrationBt.setOnClickListener {
            val dialog = DialogUtils.showDiyDialog(this,
                R.layout.layout_in_factory_dialog,
                width = ViewGroup.LayoutParams.WRAP_CONTENT,
                height = ViewGroup.LayoutParams.WRAP_CONTENT)
            val cancel_bt = dialog.findViewById<Button>(R.id.cancel_bt)
            cancel_bt.setOnClickListener {
                dialog.dismiss()
            }
            val confirm_bt = dialog.findViewById<Button>(R.id.confirm_bt)
            val password_et = dialog.findViewById<EditText>(R.id.password_et)
            confirm_bt.setOnClickListener {
                val code = password_et.text.toString()
                if (code == "654321") {
                    dialog.dismiss()
                    startActivity(Intent(/* packageContext = */ this, /* cls = */
                        CalibrationActivity::class.java))
                } else {
                    ToastUtil.showShortToast("密码错误")
                }
            }
        }
    }

    override fun createObserver() {

    }

    private fun isValidMacAddress(macAddress: String): Boolean { // 正则表达式用于匹配 MAC 地址格式
        // ^ 表示字符串开始
        // ([0-9A-Fa-f]{2}) 表示匹配两个十六进制字符（0-9, A-F, a-f）
        // ([:-]{1}) 表示匹配一个冒号或一个短划线
        // {5} 表示前面的模式重复5次 (总共6组，第一组在括号外)
        // $ 表示字符串结束
        val macAddressPattern = Regex("^([0-9A-Fa-f]{2}([:-]{1})){5}([0-9A-Fa-f]{2})$")

        return macAddressPattern.matches(macAddress)
    }
}